package com.right.health.app.ui.medical.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magic.basic.adapter.BaseAbsAdapter;
import com.magic.basic.utils.TimeUtil;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.MedicalRecordInfo;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.utils.GlideUtil;
import com.right.health.app.widget.GridViewEx;

import java.util.concurrent.TimeoutException;

public class MedicalRecordDetailAdapter extends BaseAbsAdapter<MedicalRecordInfo> {

    public MedicalRecordDetailAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.medical_record_detail_item, null);
            viewHolder = new ViewHolder();

            viewHolder.descTv =  convertView.findViewById(R.id.tv_desc);
            viewHolder.courseDiseaseTv = (TextView) convertView.findViewById(R.id.tv_course_disease);

            viewHolder.photoGrid = (GridViewEx) convertView.findViewById(R.id.photo_grid);
            viewHolder.photoAdapter = new PhotoAdapter(mContext);
            viewHolder.photoGrid.setAdapter(viewHolder.photoAdapter);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MedicalRecordInfo info = mDataSource.get(position);
        if (!TextUtils.isEmpty(info.casehistorydesc)){
            viewHolder.descTv.setText(TimeUtil.convertTime("yyyy-MM-dd", info.casehistorydesc));
        }
        viewHolder.courseDiseaseTv.setText(info.infectionprogressName);
        viewHolder.photoAdapter.setDataSource(info.photoDataSource);

        return convertView;
    }



    private class ViewHolder {

        private TextView descTv;
        private TextView courseDiseaseTv;

        private GridViewEx photoGrid;
        private PhotoAdapter photoAdapter;
    }

    private class PhotoAdapter extends BaseAbsAdapter<String> {

        PhotoAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PhotoViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.medical_record_add_photo_item, null);
                viewHolder = new PhotoViewHolder();
                viewHolder.contentImage = (ImageView) convertView.findViewById(R.id.iv_content);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (PhotoViewHolder) convertView.getTag();
            }
            String path = mDataSource.get(position);
                Glide.with(App.getInstance()).load(AppConfig.PHOTO_URL + path).apply(GlideUtil.getDefaultPlaceholder()).into(viewHolder.contentImage);

            return convertView;
        }

        private class PhotoViewHolder {
            private ImageView contentImage;
        }
    }
}
